package com.wz.dhs.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;

@Configuration
@EnableSwagger2WebMvc
@Slf4j
public class SwaggerConfig {

    // 读取当前激活的环境
    @Value("${spring.profiles.active}")
    private String activeProfile;

    @Bean
    public Docket createLoginDocker() {
        return createDocket("登录服务", "com.wz.dhs.controller.login");
    }

    @Bean
    public Docket createCommonDocker() {
        return createDocket("公共服务", "com.wz.dhs.controller.common");
    }

    @Bean
    public Docket createOrderDocker() {
        return createDocket("停车相关接口", "com.wz.dhs.controller.car");
    }

    @Bean
    public Docket createMemberDocker() {
        return createDocket("会员相关接口", "com.wz.dhs.controller.member");
    }

    /**
     * 创建通用 Docket 配置
     */
    private Docket createDocket(String groupName, String basePackage) {
        String pathPrefix = getPathPrefix(); // 获取路径前缀
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .groupName(groupName)
                .globalOperationParameters(getParameterList())
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build()
                .pathMapping(pathPrefix); // 设置路径前缀
    }

    /**
     * 根据环境和操作系统获取路径前缀
     */
    private String getPathPrefix() {
        // 判断是否为 Linux 环境
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("linux")) {
            log.info("当前操作系统环境为:linux，spring环境:{},添加前缀访问", activeProfile);
            if ("dev".equals(activeProfile)) {
                return "/dev-api";
            } else if ("prod".equals(activeProfile)) {
                return "/api";
            }
        }
        log.info("当前操作系统环境为:{}，spring环境:{},不添加前缀访问", osName,activeProfile);
        return ""; // 默认不加前缀（Windows 或其他环境）
    }

    /**
     * 添加 head 参数配置
     */
    private List<Parameter> getParameterList() {
        ParameterBuilder clientIdTicket = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        clientIdTicket.name("token").description("token令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build(); // 设置 false，表示 token 参数非必填
        pars.add(clientIdTicket.build());

        pars.add(new ParameterBuilder().name("Accept-Language").description("国际化语言")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build());
        return pars;
    }

    /**
     * API 文档信息
     */
    public ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("海聚天成停车场API文档")
                .description("海聚天成停车场API")
                .version("1.0")
                .build();
    }
}
